<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<script type="text/javascript">
	$(function() {

		$('#admin_member_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/memberController/datagrid.action?memberType=f',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'createTime',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : false,
			frozenColumns : [ [ {
				field : 'id', title : '编号', checkbox : true}, {
				field : 'cardId', title : '卡号', sortable : true} ] ],
			columns : [ [ {
				field : 'name', title : '姓名', sortable : true}, {
				field : 'pid', title : '父卡号', sortable : false}, {
				field : 'createTime', title : '创建时间', sortable : true}, {
				field : 'mobile', title : '手机号', sortable : true}, {
				field : 'email', title : '电子邮箱', sortable : true}, {
				field : 'statusName', title : '状态', sortable : true}, {
				field : 'memberTypeName', title : '会员类型', sortable : true}, {
				field : 'cid', title : '身份证号', sortable : true}, {
				field : 'genderName', title : '性别', sortable : true}, {
				field : 'payTypeName', title : '支付方式', sortable : true}, {
				field : 'regCharge', title : '缴费金额（元）', sortable : true, align : 'right'}, {
				field : 'memberFee', title : '会费（元）', sortable : true, align : 'right'}, {
				field : 'jifen', title : '积分', sortable : true, align : 'right'}, {
				field : 'balance', title : '消费余额（元）', sortable : true, align : 'right'}, {
				field : 'action1', title : '查看详情/修改', align : 'center', formatter : function(value, row, index) {
				    return formatString('<img onclick="admin_member_editFun(\'{0}\');" src="{1}"/ title="查看详情/修改">', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/pencil.png');
				}
			},
			{
				field : 'action2', title : '修改支付信息', align : 'center', formatter : function(value, row, index) {
					if(row.status == '2' || row.status == '0' || row.status == '5') {
						return formatString('<img onclick="admin_member_editPayFun(\'{0}\');" src="{1}"/ title="修改支付信息">', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/pencil.png');
					}
				}
			}]],
			toolbar : [ {
				text : '添加会员', iconCls : 'icon-add', handler : function() {
					admin_member_appendFun();
				}
			},{
				text : '添加免会费会员', iconCls : 'icon-add', handler : function() {
					admin_free_member_appendFun();
				}
			}]
		});

	});

	function admin_member_searchFun() {
		$('#admin_member_datagrid').datagrid('load', serializeObject($('#admin_member_searchForm')));
	}
	
	function admin_member_cleanFun() {
		$('#admin_member_searchForm input').val('');
		$('#admin_member_datagrid').datagrid('load', {});
	}
	
	function admin_member_editFun(id) {
		$('#admin_member_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/member_edit.jsp',
			width : 600,
			height : 500,
			modal : true,
			title : '修改会员信息',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-edit',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_memberEdit_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/memberController/edit.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_member_datagrid').datagrid('reload');
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				var index = $('#admin_member_datagrid').datagrid('getRowIndex', id);
				var rows = $('#admin_member_datagrid').datagrid('getRows');
				var o = rows[index];
				$('#admin_memberEdit_editForm').form('load', o);
			}
		});
	}
	
	function admin_member_editPayFun(id) {
		$('#admin_member_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/member_edit_pay.jsp',
			width : 600,
			height : 500,
			modal : true,
			title : '修改会员类型及支付信息',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-edit',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_memberPayEdit_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/memberController/editPay.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_member_datagrid').datagrid('reload');
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				var index = $('#admin_member_datagrid').datagrid('getRowIndex', id);
				var rows = $('#admin_member_datagrid').datagrid('getRows');
				var o = rows[index];
				$('#admin_memberPayEdit_editForm').form('load', o);
			}
		});
	}
	
	function admin_member_appendFun() {
		$('#admin_member_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/member_add.jsp?type=1',
			width : 600,
			height : 500,
			modal : true,
			title : '添加会员',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_memberAdd_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/memberController/add.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_member_datagrid').datagrid('load',{sort : 'createTime', order : 'desc'});
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	
	function admin_free_member_appendFun() {
		$('#admin_member_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/free_member_add.jsp',
			width : 600,
			height : 500,
			modal : true,
			title : '添加免会费会员',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_free_memberAdd_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/memberController/addFree.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#admin_member_datagrid').datagrid('load',{sort : 'createTime', order : 'desc'});
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	
	function admin_yhgl_removeFun() {
		var rows = $('#admin_yhgl_datagrid').datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
				if (r) {
					var currentUserId = '${sessionInfo.userId}';/*当前登录用户的ID*/
					var flag = false;
					for ( var i = 0; i < rows.length; i++) {
						if (currentUserId != rows[i].id) {
							ids.push(rows[i].id);
						} else {
							flag = true;
						}
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/userController/remove.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(result) {
							if (result.success) {
								$('#admin_yhgl_datagrid').datagrid('load');
								$('#admin_yhgl_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
							}
							if (flag) {
								$.messager.show({
									title : '提示',
									msg : '不可以删除自己！'
								});
							} else {
								$.messager.show({
									title : '提示',
									msg : result.msg
								});
							}
						}
					});
				}
			});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}
	function admin_yhgl_deleteFun(id) {
		$('#admin_yhgl_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('#admin_yhgl_datagrid').datagrid('checkRow', $('#admin_yhgl_datagrid').datagrid('getRowIndex', id));
		admin_yhgl_removeFun();
	}
	function admin_yhgl_modifyPwdFun(id) {
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/admin/yhglEditPwd.jsp',
			width : 300,
			height : 200,
			modal : true,
			title : '编辑用户密码',
			buttons : [ {
				text : '编辑',
				iconCls : 'icon-edit',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#admin_yhglEditPwd_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/userController/modifyPwd.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									d.dialog('destroy');
									$('#admin_yhgl_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				$('#admin_yhglEditPwd_editForm').form('load', {
					id : id
				});
			}
		});
	}
	function admin_yhgl_modifyRoleFun() {
		var rows = $('#admin_yhgl_datagrid').datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				ids.push(rows[i].id);
			}
			$('<div/>').dialog({
				href : '${pageContext.request.contextPath}/admin/yhglEditRole.jsp',
				width : 300,
				height : 200,
				modal : true,
				title : '批量编辑用户角色',
				buttons : [ {
					text : '编辑',
					iconCls : 'icon-edit',
					handler : function() {
						var d = $(this).closest('.window-body');
						$('#admin_yhglEditRole_editForm').form('submit', {
							url : '${pageContext.request.contextPath}/userController/modifyRole.action',
							success : function(result) {
								try {
									var r = $.parseJSON(result);
									if (r.success) {
										$('#admin_yhgl_datagrid').datagrid('load');
										$('#admin_yhgl_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
										d.dialog('destroy');
									}
									$.messager.show({
										title : '提示',
										msg : r.msg
									});
								} catch (e) {
									$.messager.alert('提示', result);
								}
							}
						});
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				},
				onLoad : function() {
					$('#admin_yhglEditRole_editForm').form('load', {
						ids : ids
					});
				}
			});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选要编辑的记录！'
			});
		}
	}
</script>
<div class="easyui-layout" data-options="fit : true,border : false">
	<div data-options="region:'north',title:'查询条件',border:false" style="height:200px" align="center">
		<form id="admin_member_searchForm">
			<table class="tableForm">
				<tr>
					<th style="width: 170px;">卡号(可模糊查询)</th>
					<td><input name="cardId" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th style="width: 170px;">姓名(可模糊查询)</th>
					<td><input name="name" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th style="width: 170px;">手机号(可模糊查询)</th>
					<td><input name="mobile" style="width: 315px;" /></td>
				</tr>
				<tr>
					<th>创建时间</th>
					<td><input name="createTimeStart" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" />至<input name="createTimeEnd" onFocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" /></td>
				</tr>
			</table>
			<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="admin_member_searchFun();">过滤</a> <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="admin_member_cleanFun();">取消过滤</a>
		</form>
	</div>
	<div data-options="region:'center',border:false">
		<table id="admin_member_datagrid"></table>
	</div>
</div>